package com.prothink.threadadmindemo.controller;

import com.prothink.threadadmindemo.entity.UserEntity;
import com.prothink.threadadmindemo.service.UserService;
import com.prothink.threadadmindemo.vo.AccountVo;
import com.prothink.threadadmindemo.vo.ForgetPwdVo;
import com.prothink.threadadmindemo.vo.Result;
import com.prothink.threadadmindemo.vo.UserInfoVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * @author mayanping
 * @title: UserController
 * @projectName ThreadAdminDemo
 * @description: TODO
 * @date 2024/9/508:48
 */
@RequestMapping("/user")
@RestController
public class UserController {
    @Autowired
    private UserService userService;

    @GetMapping("/users")
    public Result<List> getUserList(){
        List<UserEntity> users = userService.getUserList();
        return Result.success(users);
    }

    @PostMapping("/user")
    public Object addUser(@RequestBody UserEntity userEntity){
        userService.addUser(userEntity);
        return "success";
    }

    /**
     * SpringMVC接受参数的方式：
     * @RequestBody: Content-Type: application/json
     * @RequestParam: Content-Type: application/x-www-form-urlencoded from表单
     * @PathVariable:   路径上面参数
     * @param account
     */
    @PostMapping("/register")
    public Result<String> register(@Valid @RequestBody AccountVo account){
        userService.registUser(account);
        return Result.success("注册成功");
    }

    @PostMapping("/login")
    public Result<UserInfoVo> login(@Valid @RequestBody AccountVo account){
        UserInfoVo userInfoVo = userService.login(account);
        return Result.success(userInfoVo);
    }

    @PostMapping("/resetPwd")
    public Result<String> resetPwd(@Valid @RequestBody ForgetPwdVo forgetPwdVo){
        //发邮件
        userService.resetPwdWithAttachment(forgetPwdVo);
        return Result.success();
    }

    /**
     * 校验账号是否存在
     * @param forgetPwdVo
     * @return
     */
    @PostMapping("/validUserAccount")
    public Result<String> validUserAccount(@RequestBody ForgetPwdVo forgetPwdVo){
        if(StringUtils.isEmpty(forgetPwdVo.getAccount())){
            return Result.failure("账号不能为空");
        }

        UserEntity user = userService.getUserByName(forgetPwdVo.getAccount());
        if(user == null){
            return Result.failure("该账号不存在: " + forgetPwdVo.getAccount());
        }

        return Result.success();
    }

    /**
     * 校验身份证号是否存在
     * @param forgetPwdVo
     * @return
     */
    @PostMapping("/validIdCard")
    public Result<String> validIdCard(@RequestBody ForgetPwdVo forgetPwdVo){
        if(StringUtils.isEmpty(forgetPwdVo.getAccount())){
            return Result.failure("账号不能为空");
        }

        if(StringUtils.isEmpty(forgetPwdVo.getIdCard())){
            return Result.failure("身份证号不能为空");
        }

        UserEntity user = userService.getUserByName(forgetPwdVo.getAccount());
        if(user == null){
            return Result.failure("该账号不存在: " + forgetPwdVo.getAccount());
        }
        if(!user.getIdCard().equals(forgetPwdVo.getIdCard())){
            return Result.failure("身份证号验证失败: " + forgetPwdVo.getAccount());
        }

        return Result.success();
    }

}
